<?php 
/*****************
*母亲节后台接口
*
*创建日期:2012-4-13
*作者：tjx
******************/
header("expires:mon,26 jul 1997 05:00:00 gmt");  
header("cache-control:no-cache,must-revalidate");  
header("pragma:no-cache");
define('IN_WONDERSHARE', true);

if( $_GET['code'] != md5('mothers_day') ) {
	exit('error code!');
}

require( '../../inc/config.inc.php');
require( '../../includes/cls_mysql.php');
require( '../../includes/lib_common.php');

if(empty($db)) {
	$db = new cls_mysql($db_host, $db_user, $db_pass, $db_name);
}


/**
* 函数名：isUserPlace($db)
* 描述：判断当天用户玩的时间是否取得前五名
* @params $db 操作数据库对象
* @return  int (1为名次在前五名)
*/
function isUserPlace($db) {
	
	//获取当天人数
	$sql = "SELECT COUNT(1) AS nums 
			FROM ( 
			    SELECT 1 
			    FROM collect_user 
			    WHERE to_day_time = '{$_GET['day_time']}' AND web_type = '{$_GET['web_type']}'
			    GROUP BY play_time , play_steps 
			    ORDER BY play_time ASC , play_steps ASC LIMIT 5 
			)  a ";
	
	$nums = $db->getOne($sql);
	if ($nums >= 5 ) {
		
		$sql = "SELECT COUNT(1) AS nums 
				FROM ( 
				    SELECT play_time, play_steps 
				    FROM collect_user 
				    WHERE to_day_time = '{$_GET['day_time']}' AND web_type = '{$_GET['web_type']}'
				    GROUP BY play_time , play_steps 
				    ORDER BY play_time ASC , play_steps ASC LIMIT 5 
				)  a 
				WHERE a.play_time >= '{$_GET['play_time']}' AND a.play_steps >= '{$_GET['play_steps']}' ";
		
		$nums = $db->getOne($sql);
		
		echo $nums > 0 ? 1 : 0;
	} else {
		echo 1;
	}
	
}

/**
* 函数名：insertUserInfo($db)
* 描述：根据插入前五名的用户信息
* @params $db 操作数据库对象
* @return  int (1为插入成功，0为插入失败)
*/

function insertUserInfo($db) {
	
	$user_ip = $_GET['ip'];
	//判断邮箱是否存在
	$sql = "SELECT COUNT(1) AS nums 
			FROM collect_user 
			WHERE email = '{$_GET['user_email']}' AND to_day_time = '{$_GET['day_time']}' AND web_type = '{$_GET['web_type']}' ";
	
	$nums = $db->getOne($sql);
	//如果不存在
	if ($nums == 0) {
		
		$sql = "INSERT INTO collect_user(name, email, play_steps, play_time, play_str_time, ip, to_day_time, web_type) 
				VALUES ('{$_GET['user_name']}', '{$_GET['user_email']}', '{$_GET['play_steps']}', '{$_GET['play_time']}', '{$_GET['play_str_time']}', inet_aton('{$user_ip}'), '{$_GET['day_time']}', '{$_GET['web_type']}') ";
		
	
		
	} else {
		$sql = "UPDATE collect_user SET play_time = '{$_GET['play_time']}', play_steps = '{$_GET['play_steps']}', play_str_time = '{$_GET['play_str_time']}' , name = '{$_GET['user_name']}', ip = inet_aton('{$user_ip}') 
				WHERE email = '{$_GET['user_email']}' AND play_time >= '{$_GET['play_time']}' AND play_steps >= '{$_GET['play_steps']}' AND to_day_time = '{$_GET['day_time']}' AND web_type = '{$_GET['web_type']}' ";
		
	}
	
	$affect_nums = $db->query($sql);
	
	echo $affect_nums == 1 ? 1 : 0;
}

/**
* 函数名：getUserRankingInfo($db)
* 描述：前五名用户信息
* @params $db 操作数据库对象
* @return  string
*/
function getUserRankingInfo($db) {
	
	$user_array = array();
	//获取分组前5的当天用户
	$sql = "SELECT play_steps, play_time
			FROM collect_user 
			WHERE to_day_time = '{$_GET['day_time']}' AND web_type = '{$_GET['web_type']}'
			GROUP BY play_time, play_steps 
			ORDER BY play_time ASC , play_steps ASC LIMIT 5 ";
	
	$rows = $db->getAll($sql);
	
	foreach ($rows AS $keys => $values) {
		
		$sql = "SELECT name, play_str_time 
				FROM collect_user 
				WHERE play_time = '{$values['play_time']}' AND play_steps = '{$values['play_steps']}' AND to_day_time = '{$_GET['day_time']}' AND web_type = '{$_GET['web_type']}' ";
		
		$user_array[] = $db->getAll($sql);
	}
	
	echo trim(json_encode($user_array));
	
}

/**
* 函数名：getFirstThreeUser($db)
* 描述：得到某天前三名用户信息
* @params $db 操作数据库对象
* @return  string
*/
function getFirstThreeUser($db) {
	
	$user_array = array();
	//得到分组日期
	$sql = "SELECT to_day_time 
			FROM collect_user 
			WHERE to_day_time < '{$_GET['day_time']}' AND web_type = '{$_GET['web_type']}' 
			GROUP BY to_day_time DESC ";
	
	$rows = $db->getAll($sql);
	
	foreach ($rows AS $keys => $values) {
		
		//获取某个日期分组排前三的用户
		$sql = "SELECT play_steps, play_time 
				FROM collect_user 
				WHERE to_day_time = '{$values['to_day_time']}' AND web_type = '{$_GET['web_type']}'
				GROUP BY play_time , play_steps 
				ORDER BY play_time ASC , play_steps ASC LIMIT 5 ";
		
		$user_time_array = $db->getAll($sql);
		
		foreach ($user_time_array AS $user_keys => $user_values) {
			
			$sql = "SELECT name, email, is_send_email, id 
					FROM collect_user 
					WHERE play_time = '{$user_values['play_time']}' AND play_steps = '{$user_values['play_steps']}' AND to_day_time = '{$values['to_day_time']}' AND web_type = '{$_GET['web_type']}' ";
								
			$user_array[$values['to_day_time']][] = $db->getAll($sql); 
		}
		
	}
	
	echo serialize($user_array);
	
}

/**
* 函数名：updateEmailFage($db)
* 描述：修改发邮件标志
* @params $db 操作数据库对象
* @return  int
*/
function updateEmailFage($db) {
	
	//判断邮箱是否存在
	$sql = "UPDATE collect_user SET is_send_email = 1 
			WHERE id = '{$_GET['id']}' ";
	
	$affect_nums = $db->query($sql);
	
	echo $affect_nums == 1 ? 1 : 0;
	
}

$fun = $_GET['fun'];
$fun($db);
?>